package com.example.orderform.contorller;

//import com.example.orderform.return_data.Mall_Data;
import com.example.orderform.otuercall.GoodsService;
import com.example.orderform.server.AllOrderServer;

import com.example.orderform.vo.ADDress;
import com.example.orderform.vo.Order;
import com.example.orderform.vo.Shi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController

@Api(tags = "所有订单控制类")
public class AllOrderContorller {

    @Autowired
    AllOrderServer server;
    @Autowired
    GoodsService goodsService;

    @ApiOperation("所有订单分页查询接口") //描述方法
    @RequestMapping("/selectorder")

    public List<Order> selectorder(
            @RequestParam("q") int q,
            @RequestParam("j") int j,
            @RequestParam("userid") int userid
    ){

        System.out.println(userid+"==="+q+"===="+j);
        List<Order> orders=server.syselectorder(userid, q, j);
        if (orders!=null){
            return orders;
        }
        return null;
    }


    @ApiOperation("按照订单号和时间查询相对应的订单数量接口")
    @RequestMapping("/selbyorder")

    public List<Order> selbyorder(@RequestParam(value = "undertime",required = false)String undertime,
                                  @RequestParam(value = "ordernumber",required = false)String ordernumber)
    {
        List<Order> allorder = server.selbyorder(undertime,ordernumber);
        return allorder;
    }


    @ApiOperation("删除订单接口") //描述方法
    @ApiImplicitParam(name = "orderid",value = "订单ID") //参数描述
    @RequestMapping("/deleteorder")

    public int deleteorder(
            @RequestParam("orderid") int orderid
    ){
        System.out.println("删除");
        server.update(orderid);
        return 1;
    }
    @ApiOperation("状态查询订单接口") //描述方法
    @ApiImplicitParam(name = "orderstatus",value = "订单状态") //参数描述
    @RequestMapping("/mohuselect")

    public List<Order> mohuselect(
            @RequestParam("orderstatus") String orderstatus,
            @RequestParam("userid") int userid
    ){

        List<Order> orders=server.mohuselect(orderstatus,userid);

        if (orders!=null){
            System.out.println("不null"+orderstatus);
            return orders;
        }
        System.out.println("null");
        return null;
    }
    @ApiOperation("查询所有订单数量接口") //描述方法
    @RequestMapping("/juhe")
    public int juhe(@RequestParam("userid") int userid){
        return server.he(userid);
    }



    @ApiOperation("订单操作接口") //描述方法

    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderid",value = "订单id"), //参数描述
            @ApiImplicitParam(name = "auvalue",value = "订单状态按钮值") //参数描述
    })

    @RequestMapping("/caozuo")
    public void caozuo(
            @RequestParam("orderid") int orderid,
            @RequestParam("auvalue") String status
    ){
        System.out.println(orderid+"===="+status);
        if (status.equals("待发货")){
            DeliverGoodsContorller deliverGoodsContorller=new DeliverGoodsContorller();

        }else if (status.equals("评价")){
            EvaluateContorller evaluateContorller=new EvaluateContorller();

        }else if (status.equals("待付款")){
            PaymentContorller paymentContorller=new PaymentContorller();

        }else if (status.equals("待收货")){
            ReceivedContorller receivedContorller=new ReceivedContorller();

        }
    }


    //生成订单
    @RequestMapping("/insertorder")
    public void insertorder(
            @RequestParam("addressid") int addressid,
            @RequestParam("userid") int userid,
            @RequestParam("spid") int spid,
            @RequestParam("ordernumber") String ordernumber,
            @RequestParam("payway") String payway,

            @RequestParam("comitydescription") String comitydescription,
            @RequestParam("comityprices") double comityprices,
            @RequestParam("comitynumber") int comitynumber,
            @RequestParam("comitypicture")  String comitypicture,
            @RequestParam("Settlementprice") double Settlementprice,

            @RequestParam("freight") String freight,
            @RequestParam("actualpayment")  double actualpayment,
            @RequestParam("preferentialinformation")  String preferentialinformation,
            @RequestParam("specifications")  String specifications

    ){
        Date date=new Date();
        String add="";
        String dh="";
        String shr="";
        if (addressid!=0){
            ADDress adDress=server.address(addressid);
            shr=adDress.getAddressname();
            dh=adDress.getAddressphone();
            if (adDress.getProvince()!=null){
                add+=adDress.getProvince();
            }
            if (adDress.getCity()!=null){
                add+=adDress.getCity();
            }
            if (adDress.getArea()!=null){
                add+=adDress.getArea();
            }
            if (adDress.getTown()!=null){
                add+=adDress.getTown();
            }
            if (adDress.getAddressdetailed()!=null){
                add+=adDress.getAddressdetailed();
            }
//            add=adDress.getProvince()+adDress.getCity()+adDress.getArea()+adDress.getTown()+adDress.getAddressdetailed();
        }else {
            add="";
        }

        if (!payway.equals("")){
            goodsService.upkc(spid,specifications,comitynumber);
            server.orderinsert(userid, spid, ordernumber, date, "支付宝", date, null, null, "买家已付款", comitydescription, comityprices, comitynumber, comitypicture, Settlementprice, add, freight, actualpayment, preferentialinformation, "1", specifications, "退货退款", "",shr,dh);
            return;
        }
        goodsService.upkc(spid,specifications,comitynumber);
        server.orderinsert(userid, spid, ordernumber, date, "", null, null, null, "等待买家付款", comitydescription, comityprices, comitynumber, comitypicture, Settlementprice, add, freight, actualpayment, preferentialinformation, "1", specifications, "立即付款", "取消订单",shr,dh);

    }


}
